package com.study.zookeeper.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.barriers.DistributedBarrier;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class RecipesBarrier {

    static String barrier_path = "/curator_recipes_barrier_path";
    static DistributedBarrier barrier;
    private static String connectStr = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2184";
    private static RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    private static String path= "/zlx";

    public static void main(String[] args) throws Exception{

        for(int i =0;i < 5;i++){
            new Thread(new Runnable() {
                public void run() {
                    try{
                        CuratorFramework client = CuratorFrameworkFactory.builder().connectString(connectStr)
                                .retryPolicy(retryPolicy).build();
                        client.start();
                        barrier = new DistributedBarrier(client, barrier_path);
                        System.out.println(Thread.currentThread().getName() + " 号barrier设置");
                        barrier.setBarrier();
                        barrier.waitOnBarrier();
                        System.err.println("启动……");
                    }catch (Exception e){
                        System.out.println("exception======" + e);
                    }
                }
            }).start();
        }
        Thread.sleep(2000);
        barrier.removeBarrier();
    }
}
